import { editor } from "@codingame/monaco-editor";

import { FileManager } from "./FileManager";
export type ExtensionFunc = {
    setup: (this: FileManager, ed: editor.IStandaloneCodeEditor) => void;
};
export type ExtensionInstance = { name: string } & ExtensionFunc;
export type Extension<T = unknown> = (props?: T) => ExtensionInstance;

export const ExtensionManager = {
    extensions: new Set<ExtensionInstance>(),
    destroy() {
        this.extensions.clear();
    },
    registerExtension(extension: ExtensionInstance) {
        this.extensions.add(extension);
    },
    exec<Key extends keyof ExtensionFunc>(
        thisArg: ThisType<ExtensionFunc[Key]>,
        key: Key,
        ...args: Parameters<ExtensionFunc[Key]>
    ) {
        this.extensions.forEach((i) => {
            if (typeof i[key] === "function") {
                /* @ts-ignore */
                i[key].apply(thisArg, args);
            }
        });
    },
};

import { KeyBind } from "./extensions/KeyBind";
import { Save } from "./extensions/Save";
import { Theme } from "./extensions/Theme";
import { Prettier } from "./extensions/prettier";
[Save, KeyBind, Theme, Prettier].forEach((i) => {
    ExtensionManager.registerExtension(i());
});
